<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png">
      </div>
      <el-form class="login_form" v-bind:model="loginForm" v-bind:rules="loginRules" ref="loginForm">
        <el-form-item prop="userName">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.userName" placeholder="请输入用户名" ></el-input>
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input prefix-icon="el-icon-unlock" v-model="loginForm.passWord" placeholder="请输入密码" show-password></el-input>
        </el-form-item>
        <el-form-item prop="verifyCode">
          <el-input prefix-icon="el-icon-key" v-model="loginForm.verifyCode" placeholder="请输入验证码" style="width: 50%"></el-input>
          <img class="codeClass" v-bind:src="imgSrc" @click="changeCodeImg()">
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" v-on:click="checkForm('loginForm')">登录</el-button>
          <el-button type="info" v-on:click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import {getAction, postAction} from "../axios/HttpUtils";
  import Vue from 'vue';

  export default {
      name:'Login',
      data(){
        return{
          imgSrc:'',
          loginForm:{},
          loginRules:{
            userName:[
              {required: true, message: '请输入用户名', trigger: 'change '},
              {max: 20, message: '最长支持20位字符！', trigger: 'change'}
            ],
            passWord:[
              {required: true, message: '请输入密码', trigger: 'change'}
            ],
            verifyCode:[
              {required: true, message: '请输入验证码', trigger:'change'},
              {min: 4,max: 4, message: '验证码为4位', trigger: 'change'}
            ]
          },
          url:{
            getCodeUrl: '/login/getCode',
            loginUrl: '/login/doLogin'
          }
        }
      },
      created() {
        this.getCode();
      },
      methods:{
          checkForm(formName) {
            this.$refs[formName].validate((valid) => {
              if (valid) {
                this.submitForm();
              } else {
                console.log('提交失败!!');
                return false;
              }
            });
          },
          resetForm(formName) {
            this.$refs[formName].resetFields();
          },
          getCode(){
            getAction(this.url.getCodeUrl).then(res => {
              if(res.code == 200){
                this.imgSrc = res.result.code;
                this.loginForm.uuid = res.result.uuid;
              }else {
                this.$message.error(res.message);
              }

            });
          },
          submitForm(){
            postAction(this.url.loginUrl, this.loginForm).then(res => {
              console.log(res);
              if (res.code == 200) {
                Vue.ls.set("token", res.result.token);
                Vue.ls.set("userInfo",res.result);
                this.$router.push("/home");
                //选择条数统一入口，后期可配置
                Vue.ls.set("pageSizeInfo", [10, 20, 50, 100]);
              } else {
                this.$message.error(res.message);
              }
            });
          },
          changeCodeImg(){
            this.getCode();
          }
        }
      }
</script>

<style lang="less" scoped>
  .login_container {
    background-color: #2b4b6b;
    height: 100%;
  }

  .login_box {
    width: 450px;
    height: 350px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }

  .btns {
    display: flex;
    justify-content: flex-end;
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .codeClass{
    vertical-align: middle;
    position: absolute;
    right: 0px;
  }
</style>
